package cn.nbs.admin.web.servlet;

import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.com.hkr.servlet.BaseServlet;
import cn.nbs.admin.entity.Admin;
import cn.nbs.admin.service.AdminService;
import cn.nbs.category.entity.Category;


@WebServlet("/AdminServlet")
public class AdminServlet extends BaseServlet{
	private AdminService service = new AdminService();
	
	
	/**
	 * 查询所有分类
	 * @param request
	 * @param response
	 * @return
	 */
	public String findAll(HttpServletRequest request ,HttpServletResponse response){
		List<Category> list = service.findAll();
		request.setAttribute("parents", list);
		return "f:/adminjsps/admin/category/list.jsp";
		
	}
	
	
	
	
	
	
	/**
	 * 登陆
	 * @param request
	 * @param response
	 * @return
	 */
	public String login(HttpServletRequest request,HttpServletResponse response){
		/*
		 *封装表单数据
		 *业务逻辑处理
		 *页面转发 ：
		 * 
		 * 
		 */
		String name = request.getParameter("adminname");
		String pwd = request.getParameter("adminpwd");
		
		Admin admin = new Admin();
		admin.setAdminname(name);
		admin.setAdminpwd(pwd);
		
		Admin adm = service.login(admin);
		
		if (adm == null) {
			return "f:/adminjsps/login.jsp";
		}else{
			request.getSession().setAttribute("admin", adm);
			Cookie cookie = new Cookie("adminname",adm.getAdminname());
			cookie.setMaxAge(60 * 60 * 12);
			response.addCookie(cookie);
			return "f:/adminjsps/admin/index.jsp";
		}
		
		
	}
	
	
	
}
